package com.lesama.buffered;

// 读取"c.txt"中的内容，将每行进行排序后输出

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.TreeSet;

public class BufferedStreamTest1 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("day14\\c.txt"));
        TreeSet<String> set = new TreeSet<>();
        String line;
        while((line = br.readLine()) != null) {
            set.add(line);
        }
        br.close();

        // 注意bw的构造不能在读取之前，因为这样创建时发现已经存在c.txt，会覆盖其中的内容
        BufferedWriter bw = new BufferedWriter(new FileWriter("day14\\c.txt"));
        for (String s : set) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();
    }
}